package com.system.function.service;

import com.system.Tools.TimeTools;
import com.system.function.dao.SupplierDAO;
import com.system.function.object.Commodity;
import com.system.function.object.Supplier;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SupplierService {
    @Autowired
    private SupplierDAO SupplierDAO;
    @Autowired
    private CommodityService commodityService;
    @Autowired
    private TimeTools timeTools;

    //查询全部
    public List<Supplier> getAllSupplier(Supplier Supplier){
        List<Supplier> allSupplier = new ArrayList<>();
        try{
            allSupplier = SupplierDAO.getAllSupplier(Supplier);
        }catch (Exception e){
            e.printStackTrace();
        }
        return allSupplier;
    }
    //查询总数
    public Integer getCountSupplier(Supplier Supplier){
        return SupplierDAO.getCountSupplier(Supplier);
    }

    //查询
    public Supplier getSupplier(Supplier Supplier){
        Supplier SupplierSearch = null;
        try{
            SupplierSearch = SupplierDAO.getSupplier(Supplier);
        }catch (Exception e){
            e.printStackTrace();
        }
        return SupplierSearch;
    }

    //增加
    public String addSupplier(Supplier Supplier){
        String isSuccess = "false";
        try{
            Supplier.setId(timeTools.getTimeInt());
            int resultNum = SupplierDAO.addSupplier(Supplier);
            if(resultNum>0){
                isSuccess = "true";
            }
        }catch (Exception e){
            e.printStackTrace();
            return "false-has-double";
        }
        return isSuccess;
    }
    //删除
    public String delSupplier(Supplier Supplier){
        String isSuccess = "false";
        try{
            //查询当前仓库下是否存在商品
            Commodity Commodity = new Commodity();
            Commodity.setSupplierId(Supplier.getId());
            List<Commodity> allCommodity = commodityService.getAllCommodity(Commodity);
            if(null != allCommodity && allCommodity.size() > 0){
                return "false";
            }
            int resultNum  = SupplierDAO.delSupplier(Supplier);
            if(resultNum>0){
                isSuccess = "true";
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return isSuccess;
    }
    //修改
    public String updateSupplier(Supplier Supplier){
        String isSuccess = "false";
        try{
            int resultNum = SupplierDAO.updateSupplier(Supplier);
            if(resultNum>0){
                isSuccess = "true";
            }
        }catch (Exception e){
            e.printStackTrace();
            return "false-has-double";
        }
        return isSuccess;
    }
}
